package cn.yunhe.mapper;

import cn.yunhe.dto.DateAndTotal;
import cn.yunhe.dto.TotalMsgDto;
import cn.yunhe.entity.PayOrder;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author ysp
 * @since 2021-08-21
 */
@Mapper
public interface PayOrderMapper extends BaseMapper<PayOrder> {

    /**
     * 查询今天的订单信息
     * @param merchantId
     * @return
     */

    @Select("select sum(TOTAL_AMOUNT) TotalMoney,count(ID) TotalCount,sum(TOTAL_AMOUNT)/count(ID) PersonAvgMoney " +
            "from pay_order " +
            "where TO_DAYS(PAY_SUCCESS_TIME) = TO_DAYS(NOW()) and MERCHANT_ID = #{merchantId}")
    TotalMsgDto queryTodayTotal(@Param("merchantId") Long merchantId);


    /**
     * 查询昨天的订单信息
     * @param merchantId
     * @return
     */

    @Select("select sum(TOTAL_AMOUNT) TotalMoney,count(ID) TotalCount,sum(TOTAL_AMOUNT)/count(ID) PersonAvgMoney " +
            "from pay_order " +
            "where TO_DAYS(PAY_SUCCESS_TIME) = TO_DAYS(NOW()) - 1 and MERCHANT_ID = #{merchantId}")
    TotalMsgDto queryYesterdayTotal(@Param("merchantId") Long merchantId);


    /**
     * 查询近两周的
     * @param merchantId
     * @return
     */

    @Select("select if(PAY_SUCCESS_TIME!=null,PAY_SUCCESS_TIME,'0') date, sum(TOTAL_AMOUNT) count" +
            " from pay_order where DateDiff(NOW(),PAY_SUCCESS_TIME)<14 " +
            "group by PAY_SUCCESS_TIME")
    List<DateAndTotal> queryDoubleWeek(Long merchantId);
}
